package com.haikesi.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haikesi.api.base.*;
import com.haikesi.api.base.Error;
import com.haikesi.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ApcosApiUtils
 * @Author HandsomeBugs
 * @Date 2019/5/15 14:28
 * @Version 1.0
 **/
@Component
public class ApcosApiUtils {


    private static Log log = LogFactory.getLog(ApcosApiUtils.class);
    //private static String serviceUrl = "http://118.31.70.12/api";
    private static String tokenName = "apcos_token";

    private static RedisUtils redisUtils;


    //模板接口相关
//    private static String templateInterfaceUrl = "http://developer.beonelot.com:6868/api";
//    private static String templateInterfaceServiceId = "74c841f6ffb2034bc4cd12a79dfcb85e";
//    private static String templateInterfaceServiceKey = "8142e51fb24fefebc57e3efe1dfb8619";
    private static String templateToken = "";
    private static Long templateTokenTime = 0L;


    @Autowired
    public ApcosApiUtils(RedisUtils redis) {
        ApcosApiUtils.redisUtils = redis;
    }

    /**
     * @param isRefresh 是否刷新 true 就不读取缓存本地的值
     * @return
     */
    public static String getToken(boolean isRefresh) {
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        String service_key = redisUtils.get(Constants.SERVICE_KEY);
        String service_back_url = redisUtils.get(Constants.SERVICE_BACK_URL);
        return ApcosApiUtils.getToken(service_id, service_back_url, service_key, isRefresh);
    }

    /**
     * 获取 apios平台token
     *
     * @param service_id    第三方唯一标识
     * @param call_back_url 回调 URI
     * @param service_key   唯一标识对应的 key
     * @param isRefresh     是否刷新 true 就不读取缓存本地的值
     * @return token
     */
    public static String getToken(String service_id, String call_back_url, String service_key, boolean isRefresh) {
        String url =  redisUtils.get(Constants.SERVICE_URL) + "/token/getToken";//请求 url

        String token = redisUtils.get(tokenName);
        if (isRefresh || StringUtils.isBlank(token)) {
            //封装头部
            Map<String, String> headers = new HashMap<>();
            headers.put("owner", "C280000002");
            headers.put("service_id", service_id);

            JSONObject requestBody = new JSONObject();
            requestBody.put("service_id", service_id);
            requestBody.put("call_back_url", call_back_url);
            requestBody.put("service_key", service_key);
            log.info("===========请求URl:" + url);
            log.info("===========请求参数:" + requestBody.toString());
            ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
            log.info("===========ResponseEntity:" + response);
            JSONObject result = response.getBody();
            //resultCode: 0200 serice_id 登录成功 请求成功 0201 service_id 登录失败，请 检查 service_id 和service_key 是否匹配
            if (result.get("resultCode").equals("0200")) {
                HashMap<String, String> resultContent = (HashMap<String, String>) result.get("resultContent");
                String resultToken = (String) resultContent.get("token");
                String expiredTime = (String) resultContent.get("expiredTime");
                long expiredTimes = Long.parseLong(expiredTime);
                long expire = expiredTimes - System.currentTimeMillis();
                token = resultToken;
                redisUtils.set(tokenName, resultToken, expire, TimeUnit.MILLISECONDS);
            }
        }
        if (StringUtils.isBlank(token)) {
            throw new BusinessException(Type.EXCEPTION, ErrorTools.ErrorAsArrayList(new Error("token", "平台token为空")));
        }
        return token;
    }


    /**
     * 订阅账号
     *
     * @param token       token
     * @param service_id  第三方唯一标识
     * @param processTime 时间的 long 类型
     * @param account     被订阅的账号
     * @return 0000 操作成功  0001【出现异常或者被订阅账号非法(不存在)】 操作失败 0003【重复订阅】 操作失败      为空参数校验失败
     */
    public static String subscribe(String token, String service_id, String processTime, String account) {
        String url = redisUtils.get(Constants.SERVICE_URL) + "/thirdSystemSubscription/accountSubscrip";//请求 url
        if (StringUtils.isBlank(service_id)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(token)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(account)) {
            return Strings.EMPTY;
        }
        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("account", account);
        log.info("===========请求URl:" + url);
        log.info("===========请求头:" + headers.toString());
        log.info("===========请求参数:" + requestBody.toString());
        ResponseEntity<String> response = RestTemplateUtils.post(url, headers, requestBody, String.class);
        log.info("===========ResponseEntity:" + response);
        log.info("===========t:" + response.getBody().replaceAll(" ", ""));
        log.info("===========t:" + response.getBody().replaceAll(" ", "").equals("accountinvalid"));
        if (response.getBody().replaceAll(" ", "").equals("accountinvalid")) {
            return "0001";
        }
        log.info("===========t:" + response.getBody().trim().replaceAll(" ", ""));
        log.info("===========t:" + response.getBody().trim().replaceAll(" ", "").equals("accountinvalid"));
        if (response.getBody().trim().replaceAll(" ", "").equals("accountinvalid")) {
            return "0001";
        }
        log.info("========================成功返回数据============================");
        JSONObject result = JSONObject.parseObject(response.getBody());
        log.info("===========ResponseEntity:" + result);
        return result.get("resultCode").toString();
    }

    /**
     * 订阅账号
     *
     * @param account
     * @return 0000 操作成功  0001【出现异常或者被订阅账号非法(不存在)】 操作失败 0003【重复订阅】 操作失败      为空参数校验失败
     */
    public static String subscribe(String account) {
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        String token = ApcosApiUtils.getToken(false);
        String processTime = String.valueOf(System.currentTimeMillis());
        String code = ApcosApiUtils.subscribe(token, service_id, processTime, account);
        if ("0101".equals(code)) {
            token = ApcosApiUtils.getToken(true);
            code = ApcosApiUtils.subscribe(token, service_id, processTime, account);
        }
        return code;
    }


    /**
     * 获取设备
     *
     * @param token         token
     * @param service_id    第三方唯一标识
     * @param processTime   时间的 long 类型
     * @param account       被订阅的账号
     * @param lastCreatTime 设备的最后创建时间
     * @return 设备集合 {deviceList:[], lastCreatTime:1556604654000}"
     */
    public static String    getDevice(String token, String service_id, String processTime, String account, Long lastCreatTime) {
        String url = redisUtils.get(Constants.SERVICE_URL) + "/device/getDevice";//请求url
        if (StringUtils.isBlank(service_id)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(token)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(account)) {
            return Strings.EMPTY;
        }
        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("account", account);
        requestBody.put("lastCreatTime", lastCreatTime);
        log.info("===========请求URl:" + url);
        log.info("===========请求头:" + headers.toString());
        log.info("===========请求参数:" + requestBody.toString());
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
        log.info("===========ResponseEntity:" + response);
        JSONObject result = response.getBody();
        if ("0101".equals(result.get("resultCode").toString())) {
            token = ApcosApiUtils.getToken(true);
            return getDevice(token, service_id, processTime, account, lastCreatTime);
        }
        return JSON.toJSONString(result.get("resultContent"));
    }

    /**
     * 获取设备
     *
     * @param account
     * @return 设备集合 {deviceList:[], lastCreatTime:1556604654000}"
     */
    public static String getDevice(String account, Long lastCreatTime) {
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        String token = ApcosApiUtils.getToken(false);
        String processTime = String.valueOf(System.currentTimeMillis());
        return ApcosApiUtils.getDevice(token, service_id, processTime, account, lastCreatTime);
    }


    /**
     * 设备控制
     *
     * @param token         token
     * @param service_id    第三方唯一标识
     * @param processTime   时间的 long 类型
     * @param account       订阅账号
     * @param serial_num    序列号
     * @param operation     操作名
     * @param controlParams 操作参数
     * @param productId     产品编号
     * @return 0000 操作成功 请求成功 0001 操作失败 9999 请求失败 为空则是参数校验不通过
     */
    public static String sendDeviceCmd(String token, String service_id, String processTime, String account, String serial_num, String operation, String controlParams, String productId) {
        String url = redisUtils.get(Constants.SERVICE_URL) + "/device/sendDeviceCmd";//请求url
        if (StringUtils.isBlank(service_id)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(token)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(account)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(serial_num)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(operation)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(productId)) {
            return Strings.EMPTY;
        }
        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("serial_num", serial_num);
        requestBody.put("operation", operation);
        requestBody.put("controlParams", controlParams);
        requestBody.put("productId", productId);
        log.info("===========请求URl:" + url);
        log.info("===========请求头:" + headers.toString());
        log.info("===========请求参数:" + requestBody.toString());
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
        log.info("===========ResponseEntity:" + response);
        JSONObject result = response.getBody();
        return result.get("resultCode").toString();
    }


    /**
     * 控制设备
     *
     * @param account       订阅账号
     * @param serial_num    序列号
     * @param operation     操作名
     * @param controlParams 操作参数
     * @param productId     产品编号
     * @return 0000 操作成功 请求成功 0001 操作失败 9999 请求失败 为空则是参数校验不通过
     */
    public static String sendDeviceCmd(String account, String serial_num, String operation, String controlParams, String productId) {
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        String token = ApcosApiUtils.getToken(false);
        String processTime = String.valueOf(System.currentTimeMillis());
        String code = ApcosApiUtils.sendDeviceCmd(token, service_id, processTime, account, serial_num, operation, controlParams, productId);
//        if ("0101".equals(code)) {
//            token = ApcosApiUtils.getToken(true);
//            code = ApcosApiUtils.sendDeviceCmd(token, service_id, processTime, account, serial_num, operation, controlParams, productId);
//        }
        return code;
    }


    /**
     * 设备控制
     *
     * @param token         token
     * @param service_id    第三方唯一标识
     * @param processTime   时间的 long 类型
     * @param account       订阅账号
     * @param serial_num    序列号
     * @param operation     操作名
     * @param controlParams 操作参数
     * @param productId     产品编号
     * @return 0000 操作成功 请求成功 0001 操作失败 9999 请求失败 为空则是参数校验不通过
     */
    public static R sendDeviceCmd_s(String token, String service_id, String processTime, String account, String serial_num, String operation, String controlParams, String productId) {
        String url = redisUtils.get(Constants.SERVICE_URL) + "/device/sendDeviceCmd";//请求url
        if (StringUtils.isBlank(service_id)) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL, ErrorTools.ErrorAsArrayList(new Error("controlDervice", "参数校验失败")));
        }
        if (StringUtils.isBlank(token)) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL, ErrorTools.ErrorAsArrayList(new Error("controlDervice", "参数校验失败")));
        }
        if (StringUtils.isBlank(account)) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL, ErrorTools.ErrorAsArrayList(new Error("controlDervice", "参数校验失败")));
        }
        if (StringUtils.isBlank(serial_num)) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL, ErrorTools.ErrorAsArrayList(new Error("controlDervice", "参数校验失败")));
        }
        if (StringUtils.isBlank(operation)) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL, ErrorTools.ErrorAsArrayList(new Error("controlDervice", "参数校验失败")));
        }
        if (StringUtils.isBlank(productId)) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL, ErrorTools.ErrorAsArrayList(new Error("controlDervice", "参数校验失败")));
        }
        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("serial_num", serial_num);
        requestBody.put("operation", operation);
        requestBody.put("controlParams", controlParams);
        requestBody.put("productId", productId);
        log.info("===========请求URl:" + url);
        log.info("===========请求头:" + headers.toString());
        log.info("===========请求参数:" + requestBody.toString());
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
        log.info("===========ResponseEntity:" + response);
        JSONObject result = response.getBody();
        if ("0101".equals(result.get("resultCode").toString())) {
            token = ApcosApiUtils.getToken(true);
            return ApcosApiUtils.sendDeviceCmd_s(token, service_id, processTime, account, serial_num, operation, controlParams, productId);
        }
        return new R().setData(result);
    }

    /**
     * 设备控制
     *
     * @param account
     * @param serial_num
     * @param operation
     * @param controlParams
     * @param productId
     * @return
     */
    public static R sendDeviceCmd_s(String account, String serial_num, String operation, String controlParams, String productId) {
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        String token = ApcosApiUtils.getToken(false);
        String processTime = String.valueOf(System.currentTimeMillis());
        return ApcosApiUtils.sendDeviceCmd_s(token, service_id, processTime, account, serial_num, operation, controlParams, productId);
    }

    /**
     * 获取设备
     *
     * @param token       token
     * @param service_id  第三方唯一标识
     * @param processTime 时间的 long 类型
     * @param account     被订阅的账号
     * @param modeConfDO  模式类  "modeConfDO":{"account":"C280000002"}
     * @return 设备集合   { "id": "1118410836352700418", "account": "C280000002", "enable_mode": "yes", "mode_name": "dddd",  "mode_readme": null, "mode_exe_before_say": null,"mode_exe_after_say": null, "mode_exe_say_sound": null,"mode_exe_say_speed": null,"mode_exe_say_voice": null,"create_time": "2019-04-17T15:08:17","update_time": null}
     */
    public static String getMode(String token, String service_id, String processTime, String account, Object modeConfDO) {
        String url = redisUtils.get(Constants.SERVICE_URL) + "/device/getMode";//请求url
        if (StringUtils.isBlank(service_id)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(token)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(account)) {
            return Strings.EMPTY;
        }
        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("account", account);
        requestBody.put("modeConfDO", modeConfDO);
        log.info("===========请求URl:" + url);
        log.info("===========请求头:" + headers.toString());
        log.info("===========请求参数:" + requestBody.toString());
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
        log.info("===========ResponseEntity:" + response);
        JSONObject result = response.getBody();
        return JSON.toJSONString(result.get("resultContent"));
    }

    /**
     * 获取模式
     *
     * @param account    被订阅的账号
     * @param modeConfDO 模式类  "modeConfDO":{"account":"C280000002"}
     * @return 设备集合   { "id": "1118410836352700418", "account": "C280000002", "enable_mode": "yes", "mode_name": "dddd",  "mode_readme": null, "mode_exe_before_say": null,"mode_exe_after_say": null, "mode_exe_say_sound": null,"mode_exe_say_speed": null,"mode_exe_say_voice": null,"create_time": "2019-04-17T15:08:17","update_time": null}
     */
    public static String getMode(String account, Object modeConfDO) {
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        String token = ApcosApiUtils.getToken(false);
        String processTime = String.valueOf(System.currentTimeMillis());
        return ApcosApiUtils.getMode(token, service_id, processTime, account, modeConfDO);
    }

    /**
     * 添加模式
     *
     * @param account     订阅账号
     * @param token       token
     * @param service_id  第三方唯一标识
     * @param processTime 时间的long类型
     * @param modeConfDO  模式类
     * @param cmds        模式命令
     * @return 0000    操作成功	请求成功 0001	操作失败	 9999	请求失败	 3001	模式名重复
     */
    public static String addMode(String account, String token, String service_id, String processTime, Object modeConfDO, String cmds) {
        String url = redisUtils.get(Constants.SERVICE_URL) + "/mode/addMode";//请求url
        if (StringUtils.isBlank(service_id)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(token)) {
            return Strings.EMPTY;
        }
        if (modeConfDO == null) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(cmds)) {
            return Strings.EMPTY;
        }
        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("modeConfDO", modeConfDO);
        requestBody.put("cmds", JSONObject.parse(cmds));
        log.info("===========请求URl:" + url);
        log.info("===========请求头:" + headers.toString());
        log.info("===========请求参数:" + requestBody.toString());
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
        log.info("===========ResponseEntity:" + response);
        JSONObject result = response.getBody();
        if ("0101".equals(result.get("resultCode").toString())) {
            token = ApcosApiUtils.getToken(true);
            return addMode(account, token, service_id, processTime, modeConfDO, cmds);
        }
        return JSON.toJSONString(result.get("resultContent"));
    }

    //"modeConfDO":{
    //		     "account": "C280000002",
    //            "enable_mode": "yes",
    //            "mode_name": "盘管机-高风速",
    //
    //	}

    /**
     * 添加模式
     *
     * @param account    订阅账号
     * @param modeConfDO 模式类
     * @param cmds       模式命令 "cmds":["000d6f001091a72e_00000044_on_{\"p1\":\"1\"}","000d6f000cf538b2_00000029_off_none"]
     * @return 0000    操作成功	请求成功 0001	操作失败	 9999	请求失败	 3001	模式名重复
     */
    public static String addMode(String account, Object modeConfDO, String cmds) {
        log.info(">>>>>>>>>>>>>>>>" + cmds);
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        String token = ApcosApiUtils.getToken(false);
        String processTime = String.valueOf(System.currentTimeMillis());
        return ApcosApiUtils.addMode(account, token, service_id, processTime, modeConfDO, cmds);
    }


    /**
     * 修改模式
     *
     * @param account     订阅账号
     * @param token       token
     * @param service_id  第三方唯一标识
     * @param processTime 时间的long类型
     * @param modeConfDO  模式类
     * @param cmds        模式命令
     * @return 0000    操作成功	请求成功 0001	操作失败	 9999	请求失败	 3001	模式名重复
     */
    public static String updateMode(String account, String token, String service_id, String processTime, Object modeConfDO, String cmds) {
        String url = redisUtils.get(Constants.SERVICE_URL) + "/mode/updateMode";//请求url
        if (StringUtils.isBlank(service_id)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(token)) {
            return Strings.EMPTY;
        }
        if (modeConfDO == null) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(cmds)) {
            return Strings.EMPTY;
        }
        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("modeConfDO", modeConfDO);
        requestBody.put("cmds", JSONObject.parse(cmds));
        log.info("===========请求URl:" + url);
        log.info("===========请求头:" + headers.toString());
        log.info("===========请求参数:" + requestBody.toString());
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
        log.info("===========ResponseEntity:" + response);
        JSONObject result = response.getBody();
        return result.get("resultCode").toString();
    }


    /**
     * 修改模式
     *
     * @param account    订阅账号
     * @param modeConfDO 模式类
     * @param cmds       模式命令 "cmds":["000d6f001091a72e_00000044_on_{\"p1\":\"1\"}","000d6f000cf538b2_00000029_off_none"]
     * @return 0000    操作成功	请求成功 0001	操作失败	 9999	请求失败	 3001	模式名重复
     */
    public static String updateMode(String account, Object modeConfDO, String cmds) {
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        String token = ApcosApiUtils.getToken(false);
        String processTime = String.valueOf(System.currentTimeMillis());
        String code = ApcosApiUtils.updateMode(account, token, service_id, processTime, modeConfDO, cmds);
        if ("0101".equals(code)) {
            token = ApcosApiUtils.getToken(true);
            code = ApcosApiUtils.updateMode(account, token, service_id, processTime, modeConfDO, cmds);
        }
        return code;
    }


    /**
     * 控制模式
     *
     * @param account     订阅账号
     * @param token       token
     * @param service_id  第三方唯一标识
     * @param processTime 时间的long类型
     * @param modeConfDO  模式类 modeConfDO:{"id":"1116605507013009409"}
     * @return 0000   操作成功	请求成功 0001	操作失败	 9999	请求失败
     */
    public static String sendMode(String account, String token, String service_id, String processTime, Object modeConfDO) {
        String url = redisUtils.get(Constants.SERVICE_URL) + "/mode/sendMode";//请求url
        if (StringUtils.isBlank(service_id)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(account)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(token)) {
            return Strings.EMPTY;
        }
        if (modeConfDO == null) {
            return Strings.EMPTY;
        }
        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("modeConfDO", modeConfDO);
        log.info("===========请求URl:" + url);
        log.info("===========请求头:" + headers.toString());
        log.info("===========请求参数:" + requestBody.toString());
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
        log.info("===========ResponseEntity:" + response);
        JSONObject result = response.getBody();
        return result.get("resultCode").toString();
    }


    /**
     * 控制模式
     *
     * @param account    账号
     * @param modeConfDO 模式类 modeConfDO:{"id":"1116605507013009409"}
     * @return 0000   操作成功	请求成功 0001	操作失败	 9999	请求失败
     */
    public static String sendMode(String account, Object modeConfDO) {
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        String token = ApcosApiUtils.getToken(false);
        String processTime = String.valueOf(System.currentTimeMillis());
        String code = ApcosApiUtils.sendMode(account, token, service_id, processTime, modeConfDO);
        if ("0101".equals(code)) {
            token = ApcosApiUtils.getToken(true);
            code = ApcosApiUtils.sendMode(account, token, service_id, processTime, modeConfDO);
        }
        return code;
    }


    /**
     * 删除模式
     *
     * @param account     订阅账号
     * @param token       token
     * @param service_id  第三方唯一标识
     * @param processTime 时间的long类型
     * @param modeConfDO  模式类 modeConfDO:{"id":"1116605507013009409"}
     * @return 0000   操作成功	请求成功 0001	操作失败	 9999	请求失败
     */
    public static String deletMode(String account, String token, String service_id, String processTime, Object modeConfDO) {
        String url = redisUtils.get(Constants.SERVICE_URL) + "/mode/deletMode";//请求url
        if (StringUtils.isBlank(service_id)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(account)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(token)) {
            return Strings.EMPTY;
        }
        if (modeConfDO == null) {
            return Strings.EMPTY;
        }
        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("modeConfDO", modeConfDO);
        log.info("===========请求URl:" + url);
        log.info("===========请求头:" + headers.toString());
        log.info("===========请求参数:" + requestBody.toString());
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
        log.info("===========ResponseEntity:" + response);
        JSONObject result = response.getBody();
        return result.get("resultCode").toString();
    }

    /**
     * 删除模式
     *
     * @param account    账号
     * @param modeConfDO 模式类 modeConfDO:{"id":"1116605507013009409"}
     * @return 0000   操作成功	请求成功 0001	操作失败	 9999	请求失败
     */
    public static String deletMode(String account, Object modeConfDO) {
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        String token = ApcosApiUtils.getToken(false);
        String processTime = String.valueOf(System.currentTimeMillis());
        String code = ApcosApiUtils.deletMode(account, token, service_id, processTime, modeConfDO);
        if ("0101".equals(code)) {
            token = ApcosApiUtils.getToken(true);
            code = ApcosApiUtils.deletMode(account, token, service_id, processTime, modeConfDO);
        }
        return code;
    }


    /**
     *
     * ━━━━━━神兽出没━━━━━━
     * 　　　┏┓　　　┏┓
     * 　　┏┛┻━━━┛┻┓
     * 　　┃　　　　　　　┃
     * 　　┃　　　━　　　┃
     * 　　┃　┳┛　┗┳　┃
     * 　　┃　　　　　　　┃
     * 　　┃　　　┻　　　┃
     * 　　┃　　　　　　　┃
     * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
     * 　　　　┃　　　┃    神兽保佑,代码无bug
     * 　　　　┃　　　┃
     * 　　　　┃　　　┗━━━┓
     * 　　　　┃　　　　　　　┣┓
     * 　　　　┃　　　　　　　┏┛
     * 　　　　┗┓┓┏━┳┓┏┛
     * 　　　　　┃┫┫　┃┫┫
     * 　　　　　┗┻┛　┗┻┛
     *
     */

    /**
     * 获取模板Token
     *
     * @return
     */
    public static String getTemplateToken() {
        String templateInterfaceUrl= redisUtils.get(Constants.KF_SERVICE_URL);
        String templateInterfaceServiceId = redisUtils.get(Constants.KF_SERVICE_ID);
        String templateInterfaceServiceKey = redisUtils.get(Constants.KF_SERVICE_KEY);

        Long nowDate = new Date().getTime();
        if ("".equals(templateToken) || nowDate >= templateTokenTime) {
            String url = templateInterfaceUrl + "/token/getToken";
            String service_id = templateInterfaceServiceId;
            String service_key = templateInterfaceServiceKey;
            String call_back_url = "1";

            //封装参数
            JSONObject requestBody = new JSONObject();
            requestBody.put("service_id", service_id);
            requestBody.put("service_key", service_key);
            requestBody.put("call_back_url", call_back_url);
            log.info("===========请求URl:" + url);
            log.info("===========请求参数:" + requestBody.toString());
            ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, requestBody, JSONObject.class);

            log.info("===========ResponseEntity===========:" + response);
            JSONObject result = response.getBody();
            if ("0200".equals(result.get("resultCode"))) {
                LinkedHashMap<String, String> linkedHashMap = (LinkedHashMap<String, String>) result.get("resultContent");
                templateTokenTime = Long.parseLong(linkedHashMap.get("expiredTime").toString());
                templateToken = linkedHashMap.get("token").toString();
            } else {
                throw new BusinessException(0201, "service_id登录失败，请检查service_id和service_key是否匹配");
            }
            return templateToken;
        } else {
            return templateToken;
        }
    }

    /**
     * 获取设备分类
     *
     * @return
     */
    public static String getDeviceType() {
        String templateInterfaceUrl= redisUtils.get(Constants.KF_SERVICE_URL);
        String templateInterfaceServiceId = redisUtils.get(Constants.KF_SERVICE_ID);
        String templateInterfaceServiceKey = redisUtils.get(Constants.KF_SERVICE_KEY);

        String service_id = redisUtils.get(Constants.SERVICE_ID);
        //请求url
        String url = templateInterfaceUrl + "/prodcut/getProdcutType";
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", getTemplateToken());
        requestBody.put("service_id", templateInterfaceServiceId);
        log.info("===========请求URl:" + url);
        log.info("===========请求参数:" + requestBody.toString());
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, requestBody, JSONObject.class);

        log.info("===========ResponseEntity===========:" + response);
        JSONObject result = response.getBody();
        if ("0101".equals(result.get("resultCode").toString())) {
            templateToken = "";
            requestBody.put("token", getTemplateToken());
            response = RestTemplateUtils.post(url, requestBody, JSONObject.class);
            result = response.getBody();
        }
        return JSON.toJSONString(result.get("resultContent"));
    }


    /**
     * 获取模板信息列表
     *
     * @param product_code 设备类型
     * @param search       搜索条件
     * @return
     */
    public static String getTemplate(String product_code, String search) {
        String templateInterfaceUrl= redisUtils.get(Constants.KF_SERVICE_URL);
        String templateInterfaceServiceId = redisUtils.get(Constants.KF_SERVICE_ID);
        String templateInterfaceServiceKey = redisUtils.get(Constants.KF_SERVICE_KEY);
        //请求url
        String url = templateInterfaceUrl + "/template/getTemplateList";
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", getTemplateToken());
        requestBody.put("product_code", product_code);
        requestBody.put("service_id", templateInterfaceServiceId);
        requestBody.put("processTime", String.valueOf(System.currentTimeMillis()));
        if (!search.isEmpty()) {
            requestBody.put("search", search);
        }
        log.info("===========请求URl:" + url);
        log.info("===========请求参数:" + requestBody.toString());
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, requestBody, JSONObject.class);

        log.info("===========ResponseEntity===========:" + response);
        JSONObject result = response.getBody();
        if ("0101".equals(result.get("resultCode").toString())) {
            templateToken = "";
            requestBody.put("token", getTemplateToken());
            response = RestTemplateUtils.post(url, requestBody, JSONObject.class);
            result = response.getBody();
        }
        if ("0000".equals(result.get("resultCode"))) {
            return JSON.toJSONString(result.get("resultContent"));
        } else {
            throw new BusinessException(Integer.parseInt(result.get("resultCode").toString()), result.get("resultDesc").toString());
        }
    }

    /**
     * 获取模板代码块
     *
     * @param templateId 模板ID
     * @return
     */
    public static String getTemplateCode(String templateId) {
        String templateInterfaceUrl= redisUtils.get(Constants.KF_SERVICE_URL);
        String templateInterfaceServiceId = redisUtils.get(Constants.KF_SERVICE_ID);
        String templateInterfaceServiceKey = redisUtils.get(Constants.KF_SERVICE_KEY);
        //请求url
        String url = templateInterfaceUrl + "/template/getTemplate";
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", getTemplateToken());
        requestBody.put("service_id", templateInterfaceServiceId);
        requestBody.put("template_id", templateId);

        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, requestBody, JSONObject.class);

        log.info("===========ResponseEntity===========:" + response);
        JSONObject result = response.getBody();
        if ("0101".equals(result.get("resultCode").toString())) {
            templateToken = "";
            requestBody.put("token", getTemplateToken());
            response = RestTemplateUtils.post(url, requestBody, JSONObject.class);
            result = response.getBody();
        }
        if ("0000".equals(result.get("resultCode").toString())) {
            return JSON.toJSONString(result.get("resultContent"));
        }
        throw new BusinessException(Type.EXCEPTION_FAIL, ErrorTools.ErrorAsArrayList(new Error("controlDervice", "操作失败,请稍后在试")));
    }


    /**
     * 获取定时任务列表接口
     *
     * @param account 账号
     * @return
     */
    public static String getTimerTask(String account) {
        //请求url
        String url = redisUtils.get(Constants.SERVICE_URL) + "/timer/searchByAccount";
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        String token = ApcosApiUtils.getToken(false);
        if (StringUtils.isBlank(token)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(service_id)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(account)) {
            return Strings.EMPTY;
        }

        //路径和参数
        StringBuilder sb = new StringBuilder();
        sb.append(url);
        sb.append("?account=" + account + "&token=" + token + "&service_id=" + service_id + "&processTime=" + new Date().getTime());

        ResponseEntity<JSONObject> response = RestTemplateUtils.get(sb.toString(), JSONObject.class);

        log.info("===========ResponseEntity===========:" + response);
        JSONObject result = response.getBody();
        if ("0000".equals(result.get("resultCode").toString())) {
            return JSON.toJSONString(result.get("resultContent"));
        }
        throw new BusinessException(Type.EXCEPTION_FAIL, ErrorTools.ErrorAsArrayList(new Error("controlDervice", "操作失败,请稍后在试")));
    }

    /**
     * 添加定时任务接口
     *
     * @param account       账号
     * @param timerConfInfo 定时任务信息
     * @param timerModeList 定时模式信息
     * @return
     */
    public static JSONObject addTimerTask(String account, String token, String service_id, String processTime, Object timerConfInfo, Object timerModeList) {
        //请求url
        String url = redisUtils.get(Constants.SERVICE_URL) + "/timer/add";
        if (StringUtils.isBlank(token)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(service_id)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(account)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }

        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("account", account);
        requestBody.put("timerConfInfo", timerConfInfo);
        requestBody.put("timerModeList", timerModeList);
        System.out.println("头部参数：" + headers);
        System.out.println(requestBody);
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);

        log.info("===========ResponseEntity===========:" + response);
        JSONObject result = response.getBody();
        return result;
    }

    /**
     * 添加定时任务接口
     *
     * @param account       账号
     * @param timerConfInfo 定时任务信息
     * @param timerModeList 定时模式信息
     * @return
     */
    public static JSONObject addTimerTask(String account, Object timerConfInfo, Object timerModeList) {
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        String token = ApcosApiUtils.getToken(false);
        String processTime = String.valueOf(System.currentTimeMillis());
        JSONObject code = ApcosApiUtils.addTimerTask(account, token, service_id, processTime, timerConfInfo, timerModeList);
        if ("0101".equals(code.get("resultCode").toString())) {
            token = ApcosApiUtils.getToken(true);
            code = ApcosApiUtils.addTimerTask(account, token, service_id, processTime, timerConfInfo, timerModeList);
        }
        return code;
    }


    /**
     * 修改定时任务接口
     *
     * @param account       账号
     * @param timerID       定时ID
     * @param timerConfInfo 定时任务信息
     * @param timerModeList 定时模式信息
     * @return
     */
    public static String editTimerTask(String account, String token, String service_id, String processTime, String timerID, Object
            timerConfInfo, Object timerModeList) {
        //请求url
        String url = redisUtils.get(Constants.SERVICE_URL) + "/timer/updateByTimerId";
        if (StringUtils.isBlank(token)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(service_id)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(account)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(timerID)) {
            return Strings.EMPTY;
        }

        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("account", account);
        requestBody.put("timerId", timerID);
        requestBody.put("timerConfInfo", timerConfInfo);
        requestBody.put("timerModeList", timerModeList);
        System.out.println(requestBody);
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);

        log.info("===========ResponseEntity===========:" + response);
        JSONObject result = response.getBody();
        return result.get("resultCode").toString();
    }

    /**
     * 修改定时任务接口
     *
     * @param account       账号
     * @param timerID       定时任务ID
     * @param timerConfInfo 定时任务信息
     * @param timerModeList 定时模式信息
     * @return
     */
    public static String editTimerTask(String account, String timerID, Object timerConfInfo, Object timerModeList) {
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        String token = ApcosApiUtils.getToken(false);
        String processTime = String.valueOf(System.currentTimeMillis());
        String code = ApcosApiUtils.editTimerTask(account, token, service_id, processTime, timerID, timerConfInfo, timerModeList);
        if ("0101".equals(code)) {
            token = ApcosApiUtils.getToken(true);
            code = ApcosApiUtils.editTimerTask(account, token, service_id, processTime, timerID, timerConfInfo, timerModeList);
        }
        return code;
    }

    /**
     * 删除定时任务接口
     *
     * @param account     账号
     * @param token       token
     * @param service_id  service_id
     * @param processTime 时间的long类型
     * @param timerID     定时任务ID
     * @return
     */
    public static String delTimerTask(String account, String token, String service_id, String processTime, String timerID) {
        //请求url
        String url = redisUtils.get(Constants.SERVICE_URL) + "/timer/deleteByTimerId";
        if (StringUtils.isBlank(token)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(service_id)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(processTime)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(account)) {
            return Strings.EMPTY;
        }
        if (StringUtils.isBlank(timerID)) {
            return Strings.EMPTY;
        }

        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("account", account);
        requestBody.put("timerId", timerID);

        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);

        log.info("===========ResponseEntity===========:" + response);
        JSONObject result = response.getBody();
        return result.get("resultCode").toString();
    }

    /**
     * 删除定时任务接口
     *
     * @param account 账号
     * @param timerID 定时任务ID
     * @return
     */
    public static String delTimerTask(String account, String timerID) {
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        String token = ApcosApiUtils.getToken(false);
        String processTime = String.valueOf(System.currentTimeMillis());
        String code = ApcosApiUtils.delTimerTask(account, token, service_id, processTime, timerID);
        if ("0101".equals(code)) {
            token = ApcosApiUtils.getToken(true);
            code = ApcosApiUtils.delTimerTask(account, token, service_id, processTime, timerID);
        }
        return code;
    }


    /**
     * 添加执行计划接口
     *
     * @param account   账号
     * @param plan_name 执行计划名称
     * @param enable    是否启用,默认为启用，1为禁用，2为启用
     * @param ser_code  序号
     * @return
     */
    public static JSONObject addExcutionPlan(String account, String token, String service_id, String processTime,
                                             String plan_name, String enable, String ser_code) {
        //请求url
        String url = redisUtils.get(Constants.SERVICE_URL) + "/excutionPlan/addExcutionPlan";
        if (StringUtils.isBlank(token)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(service_id)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(account)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }

        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("account", account);
        requestBody.put("plan_name", plan_name);
        requestBody.put("ser_code", ser_code);
        requestBody.put("enable", enable);
        System.out.println("头部参数：" + headers);
        System.out.println(requestBody);

        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);

        log.info("===========ResponseEntity===========:" + response);
        JSONObject result = response.getBody();
        return result;
    }

    /**
     * 添加执行计划接口
     *
     * @param account   账号
     * @param plan_name 执行计划名称
     * @param enable    是否启用,默认为启用，1为禁用，2为启用
     * @param ser_code  序号
     * @return
     */
    public static JSONObject addExcutionPlan(String account, String plan_name, String enable, String ser_code) {
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        String token = ApcosApiUtils.getToken(false);
        String processTime = String.valueOf(System.currentTimeMillis());
        JSONObject code = ApcosApiUtils.addExcutionPlan(account, token, service_id, processTime, plan_name, enable, ser_code);
        if ("0101".equals(code.get("resultCode").toString())) {
            token = ApcosApiUtils.getToken(true);
            code = ApcosApiUtils.addExcutionPlan(account, token, service_id, processTime, plan_name, enable, ser_code);
        }
        return code;
    }

    /**
     * 修改执行计划接口
     *
     * @param account   账号
     * @param plan_name 执行计划名称
     * @param enable    是否启用,默认为启用，1为禁用，2为启用
     * @param ser_code  序号
     * @param id        执行计划id
     * @return
     */
    public static JSONObject updateExcutionPlan(String account, String plan_name, String enable, String ser_code, String id) {
        //请求url
        String url = redisUtils.get(Constants.SERVICE_URL) + "/excutionPlan/updateExcutionPlanById";
        //请求service_id
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        //请求processTime
        String processTime = String.valueOf(System.currentTimeMillis());
        //请求token
        String token = ApcosApiUtils.getToken(false);

        if (StringUtils.isBlank(token)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(service_id)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(account)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }

        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("account", account);
        requestBody.put("plan_name", plan_name);
        requestBody.put("ser_code", ser_code);
        requestBody.put("enable", enable);
        requestBody.put("id", id);
        System.out.println("头部参数：" + headers);
        System.out.println("请求参数：" + requestBody);
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
        log.info("===========ResponseEntity===========:" + response);
        JSONObject result = response.getBody();
        if ("0101".equals(result.get("resultCode").toString())) {
            token = ApcosApiUtils.getToken(true);
            response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
            result = response.getBody();
        }
        return result;
    }

    /**
     * 删除执行计划接口
     *
     * @param account 账号
     * @param id      执行计划id
     * @return
     */
    public static JSONObject delExcutionPlan(String account, String id) {
        //请求url
        String url = redisUtils.get(Constants.SERVICE_URL) + "/excutionPlan/deleteExcutionPlanById";
        //请求service_id
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        //请求processTime
        String processTime = String.valueOf(System.currentTimeMillis());
        //请求token
        String token = ApcosApiUtils.getToken(false);

        if (StringUtils.isBlank(token)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(service_id)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(account)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }

        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("id", id);
        System.out.println("头部参数：" + headers);
        System.out.println("请求参数：" + requestBody);
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
        log.info("===========ResponseEntity===========:" + response);
        JSONObject result = response.getBody();
        if ("0101".equals(result.get("resultCode").toString())) {
            token = ApcosApiUtils.getToken(true);
            response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
            result = response.getBody();
        }
        return result;
    }


    /**
     * 查询执行计划接口
     *
     * @param account   账号
     * @return
     */
    public static JSONObject queryExcutionPlanByCondition(String account) {
        //请求url
        String url = redisUtils.get(Constants.SERVICE_URL) + "/excutionPlan/queryExcutionPlanByCondition";
        //请求service_id
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        //请求processTime
        String processTime = String.valueOf(System.currentTimeMillis());
        //请求token
        String token = ApcosApiUtils.getToken(false);

        if (StringUtils.isBlank(token)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(service_id)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(account)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }

        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("account", account);
        System.out.println("头部参数：" + headers);
        System.out.println("请求参数：" + requestBody);
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
        log.info("===========ResponseEntity===========:" + response);
        JSONObject result = response.getBody();
        if ("0101".equals(result.get("resultCode").toString())) {
            token = ApcosApiUtils.getToken(true);
            response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
            result = response.getBody();
        }
        return result;
    }

    /**
     * 新增执行计划进程
     *
     * @param account                   账号
     * @param plan_id                   执行计划ID
     * @param process_name              执行计划进程名称
     * @param has_condition             是否前置动作，其他进程的id
     * @param pre_trigger_time          前置时间，单位s
     * @param mode_ids                  模式id字符串，用,隔开
     * @param mode_desc                 模式中文描述，用，隔开
     * @param delay_trigger_time        延时时间，单位s
     * @param trigger_conditon_desc     条件描述
     * @param excutionConditionList     触发条件集合
     * @param excutionTimeConditionList 时间条件集合
     * @param ser_order                 序号
     * @return
     */
    public static JSONObject addExcutionPlanProcess(String account, String plan_id, String process_name, String has_condition,
                                                    String pre_trigger_time, String mode_ids, String mode_desc,
                                                    String delay_trigger_time, String trigger_conditon_desc,
                                                    String excutionConditionList, String excutionTimeConditionList,
                                                    String ser_order) {
        //请求url
        String url = redisUtils.get(Constants.SERVICE_URL) + "/excutionPlanProcess/addExcutionPlanProcess";
        //请求service_id
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        //请求processTime
        String processTime = String.valueOf(System.currentTimeMillis());
        //请求token
        String token = ApcosApiUtils.getToken(false);

        if (StringUtils.isBlank(token)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(service_id)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(account)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }

//        JSONObject jsonObject = JSON.parseObject(excutionTimeConditionList);
        JSONArray jsonArray = JSONObject.parseArray(excutionTimeConditionList);
        JSONArray jsonArray1 = JSONObject.parseArray(excutionConditionList);


        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("process_name", process_name);
        requestBody.put("ser_order", ser_order);
        requestBody.put("trigger_conditon_desc", trigger_conditon_desc);
        requestBody.put("plan_id", plan_id);
        requestBody.put("has_condition", has_condition);
        requestBody.put("mode_ids", mode_ids);
        requestBody.put("delay_trigger_time", delay_trigger_time);
        requestBody.put("excutionConditionList", jsonArray1);
        requestBody.put("excutionTimeConditionList", jsonArray);
        requestBody.put("pre_trigger_time", pre_trigger_time);
        requestBody.put("mode_desc", mode_desc);
        System.out.println("头部参数：" + headers);
        System.out.println("请求参数：" + requestBody);
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
        log.info("===========ResponseEntity===========:" + response);
        JSONObject result = response.getBody();
        if ("0101".equals(result.get("resultCode").toString())) {
            token = ApcosApiUtils.getToken(true);
            response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
            result = response.getBody();
        }
        return result;
    }

    /**
     * 修改执行计划进程
     *
     * @param account                   账号
     * @param plan_id                   执行计划ID
     * @param process_name              执行计划进程名称
     * @param has_condition             是否前置动作，其他进程的id
     * @param pre_trigger_time          前置时间，单位s
     * @param mode_ids                  模式id字符串，用,隔开
     * @param mode_desc                 模式中文描述，用，隔开
     * @param delay_trigger_time        延时时间，单位s
     * @param trigger_conditon_desc     条件描述
     * @param excutionConditionList     触发条件集合
     * @param excutionTimeConditionList 时间条件集合
     * @param ser_order                 序号
     * @param id                        进程ID
     * @return
     */
    public static JSONObject updateExcutionPlanProcess(String account, String plan_id, String process_name, String has_condition,
                                                       String pre_trigger_time, String mode_ids, String mode_desc,
                                                       String delay_trigger_time, String trigger_conditon_desc,
                                                       String excutionConditionList, String excutionTimeConditionList,
                                                       String ser_order, String id) {
        //请求url
        String url = redisUtils.get(Constants.SERVICE_URL) + "/excutionPlanProcess/updateExcutionPlanProcess";
        //请求service_id
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        //请求processTime
        String processTime = String.valueOf(System.currentTimeMillis());
        //请求token
        String token = ApcosApiUtils.getToken(false);

        if (StringUtils.isBlank(token)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(service_id)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(account)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }


        JSONArray jsonArray = JSONObject.parseArray(excutionTimeConditionList);
        JSONArray jsonArray1 = JSONObject.parseArray(excutionConditionList);

        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("id", id);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("process_name", process_name);
        requestBody.put("ser_order", ser_order);
        requestBody.put("trigger_conditon_desc", trigger_conditon_desc);
        requestBody.put("plan_id", plan_id);
        requestBody.put("has_condition", has_condition);
        requestBody.put("mode_ids", mode_ids);
        requestBody.put("delay_trigger_time", delay_trigger_time);
        requestBody.put("excutionConditionList", jsonArray1);
        requestBody.put("excutionTimeConditionList", jsonArray);
        requestBody.put("pre_trigger_time", pre_trigger_time);
        requestBody.put("mode_desc", mode_desc);
        System.out.println("头部参数：" + headers);
        System.out.println("请求参数：" + requestBody);
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
        log.info("===========ResponseEntity===========:" + response);
        JSONObject result = response.getBody();
        if ("0101".equals(result.get("resultCode").toString())) {
            token = ApcosApiUtils.getToken(true);
            response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
            result = response.getBody();
        }
        return result;
    }

    /**
     * 查询执行计划进程
     *
     * @param account 账号
     * @param plan_id 执行计划ID
     * @param id      进程id
     * @return
     */
    public static JSONObject queryExcutionPlanProcess(String account, String plan_id, String id) {
        //请求url
        String url = redisUtils.get(Constants.SERVICE_URL) + "/excutionPlanProcess/updateExcutionPlanProcess";
        //请求service_id
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        //请求processTime
        String processTime = String.valueOf(System.currentTimeMillis());
        //请求token
        String token = ApcosApiUtils.getToken(false);

        if (StringUtils.isBlank(token)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(service_id)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(account)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }

        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("plan_id", plan_id);
        requestBody.put("id", id);
        System.out.println("头部参数：" + headers);
        System.out.println("请求参数：" + requestBody);
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
        log.info("===========ResponseEntity===========:" + response);
        JSONObject result = response.getBody();
        if ("0101".equals(result.get("resultCode").toString())) {
            token = ApcosApiUtils.getToken(true);
            response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
            result = response.getBody();
        }
        return result;
    }

    /**
     * 删除执行计划进程
     *
     * @param account 账号
     * @param id      进程id
     * @return
     */
    public static JSONObject deleteExcutionPlanProcess(String account, String id) {
        //请求url
        String url = redisUtils.get(Constants.SERVICE_URL) + "/excutionPlanProcess/deleteExcutionPlanProcess";
        //请求service_id
        String service_id = redisUtils.get(Constants.SERVICE_ID);
        //请求processTime
        String processTime = String.valueOf(System.currentTimeMillis());
        //请求token
        String token = ApcosApiUtils.getToken(false);

        if (StringUtils.isBlank(token)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(service_id)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }
        if (StringUtils.isBlank(account)) {
            throw new BusinessException(Type.APCOS_TIMEOUT);
        }

        //封装头部
        Map<String, String> headers = new HashMap<>();
        headers.put("owner", account);
        headers.put("service_id", service_id);
        //封装参数
        JSONObject requestBody = new JSONObject();
        requestBody.put("token", token);
        requestBody.put("service_id", service_id);
        requestBody.put("processTime", processTime);
        requestBody.put("id", id);
        System.out.println("头部参数：" + headers);
        System.out.println("请求参数：" + requestBody);
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
        log.info("===========ResponseEntity===========:" + response);
        JSONObject result = response.getBody();
        if ("0101".equals(result.get("resultCode").toString())) {
            token = ApcosApiUtils.getToken(true);
            response = RestTemplateUtils.post(url, headers, requestBody, JSONObject.class);
            result = response.getBody();
        }
        return result;
    }
}
